package pt.utl.ist.calcNotas;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;



public class FantasticAdaptertheSecond extends ArrayAdapter<Cadeira> {

    private Context context;
    private Activity activity;
    private static LayoutInflater inflater = null;
    ArrayList<Cadeira> cadeiras;

    public FantasticAdaptertheSecond(Activity activity, Context context, ArrayList<Cadeira> cadeiras){
        super(context, R.layout.componente_fantastico_segundo, cadeiras);
        this.activity = activity;
        this.context = context;
        this.cadeiras = cadeiras;
    }

    @Override
    public View getView(final int i, View view, ViewGroup parent) {

        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.componente_fantastico_segundo, parent, false);
        TextView txt = (TextView)v.findViewById(R.id.nomeCadeiramenu);
        txt.setText(cadeiras.get(i).getNome());
        ImageButton btn = (ImageButton)v.findViewById(R.id.imageButton);



        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder adb = new AlertDialog.Builder(context);
                adb.setTitle("Eliminar "+cadeiras.get(i).toString()+".");
                adb.setMessage("Tem a certeza que deseja eliminar a disciplina "+cadeiras.get(i).toString()+"?")
                        .setCancelable(false)
                        .setPositiveButton("Eliminar", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int id) {

                                Toast.makeText(activity, "A disciplina " + cadeiras.get(i).toString() + " foi eliminada com sucesso!",
                                        Toast.LENGTH_LONG).show();
                                cadeiras.remove(i);
                                notifyDataSetChanged();
                            }

                        })
                        .setNegativeButton("Cancelar",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                Toast.makeText(activity, R.string.cancelar,
                                        Toast.LENGTH_LONG).show();}
                        });
                AlertDialog dialog = adb.create();
                dialog.show();
            }
        });

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent nextScreen = new Intent(context, CadeiraFantastica.class);
                nextScreen.putExtra("cadeiras", cadeiras);
                nextScreen.putExtra("id", i);
                activity.startActivity(nextScreen);
            }
        });
        return v;
    }
}

